<?php

namespace app\api\logic;
use app\common\basics\Logic;

class JiamiLogic extends Logic
{
//    使用自己的RSA私钥和汇付宝的公钥进行RSA加密
  public static function RSA_jiami($sign_string,$public = null,$type = '1'){

      // 设置自己的私钥
      $private_key_str =
          "-----BEGIN PRIVATE KEY-----
MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDjsxfcLtBxd/FBxojCnm5X4bTWwSRZc+nP547wAJXtUnhIErtRm4rZWBo7l8Cq8zo0oHc+b4UmSoEYnf2f8Ize82AqmaPOVdskMHZGfgnkHSoH/zUdYOl1uPjfB33xZBPLS7Oll6sLw08eW5Vzw2FOvZj4Fj7xZO504+b1i6ZI4vv0Qu05AKsg8O8E9+s+j1YV8oD37PWBwELAm6dpPMn1xS0pjLukeeZ1qDwUFwFfZlDkcwNm8f6JKi1r3mFQX3EqOKermjxr7cSmvLxsqBF8sQOsvX5MTmnoIiqZfX8F4JIRURs0fjtAXCsFBQqGv4t5He4IknROZw7/E72kFcsjAgMBAAECggEAEgyhSI/hcA0s76TYNG0wnADFP/zBb+HTV+qoFvhE6jIOXNA3TwnAYOgt9ClcWXC8XW2W5jka2eUwBI6Ji0MbN5UdO2NAa9u8akr5JS9ag12YdlfRkwmal/1YpP1IC3l+ZDAVBnxWT7FktShXWXzUWtSGqqvAw4vXpyZlqXMvdDl7rEzBt/LPnRai0s8pfOrnb5QiFk4uTXc0P4sbagDyH/Ofqr1VLxKNQvKjwp2u7I9B/NTEVeC2Es7CfKBu6zPcYfd+ymkh0VF8V7t3rzYqjWKSBGAsWU3s7NUww+SjCtpiPH7OTZ9/Ki3NRGi3Z2+8LVit3JT2saWlLdD5TA8dCQKBgQD2pAFIdZbRSQGkkffpLkn1gSx3P0jmFS+PDLrRC2CRobVKHu7NfWAbb6MnZUPebv0f7/6PnaPEB5eLqlGbPoaUz/4OyrjIHy1o1pO6ytdQUPBwnrvOyj6zYuLuVcMC9RzSfNQ2LOARjKhM8KGniC4s3ao0bM9seADUq2bpl6tKBwKBgQDsVxW8YdEPnGT4rME2inYlLVwn25157nycTtcuUptptcsDyyWMKvUdGKbgFyyMujIrewutwUVcYkoUdOCFCl4hGp3nlI15G1wYJLsYdrRVLOEtP6tpwYSjgtOhuWKGvBVydzbt+iy8LDB4T1NhaPPYqkq3IbVd8hOyDM47umWfBQKBgAO0ArDr0w/hYLzv1CEUKNge0GQZ9yY9OzGSp1tsKE8HdoleXYeIUmEXFAQLaTF1yScpm1DctRDEbGoUbJVAIxUo6k+ei5e1oCDq19z+Hq1+CZ4nQ/qe3sIiwfWCJCNvOreOWEFX+yVIT16YXlT9nnPeACg5WgPu6VRIHotzU4cRAoGBAL0BS+2yoFmGri09Iut2QdOreeaaOE75zYfHeUifYZBhhTDMdoOpGwsg2tudTNBiW5rwNT/Ir6qZJDtF2QQsbWi1+4/m+lje64CXZ1TCIad4U8A3CeNykpudFZhhRI0Rv2kiM7iPeNUn1b9xWPIu18vvK/9Wos+1t9CNcyTLBVftAoGANhG/+S36X5qiGJ9POJ97oXx7D+dmOsdy8HOIY7wCG9+kjXZRgzv9Gh44uRnZoNTlG5OKrCo9aDeJKCODgvY3aaG4e6p3+aCocrwuBTXnc5BxjiUsye6mos8YCoX5l4MXEAAx5ERSZSwdBMis+2eJUhSeqpqZr8xi407BnKjhF6s=
-----END PRIVATE KEY-----";

      if (empty($public)){
          // 设置对方的公钥
          $partner_public_key_str ="-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAw6u9bHPvnJYOzY8gtFdy
8EHBaz+HE4xo9AB9wnD5TGSR/V1qkOKS8Msz17peHX4cJOkiNj9eI0gMbXFpfKLG
qj8znpMVIPLPCBoIsbeBLiedpd/OiHLCSTE8yHyw2s7qR+aXfN4sd3pWKG7v00KZ
d4CF/kKQgKQ1K8F001w8vCEawKI39VUYHMi9v/mR1nKOx3ARNjJfXYEJ0OIs9h/4
G3CJy0YYEzpsMM3/Nmd/NXwUsb6qkN5+QXMNnqqzUWEprY2Zr9AcAo51OJ3ZjsWU
XteTwRliTotcK59roDQBOvmMCcRlkOW/3xNGcngb90M3p2o0pVjyup+HzoF5DAjr
ewIDAQAB
-----END PUBLIC KEY-----";

      }else{
          $partner_public_key_str = $public;
      }


    //分段加密方法
    //获取配置密钥
    $publicKey = $partner_public_key_str;
    // 要加密的数据是    $data

    $data = $sign_string;

    if ($type == 1){
        // 定义每个分段的大小
        $chunkSize = 117; // 2048-bit key can encrypt 117 bytes
        // 分段加密
        $encryptedData = '';
        for ($i = 0; $i < strlen($data); $i += $chunkSize) {
            $chunk = substr($data, $i, $chunkSize);
            openssl_public_encrypt($chunk, $encryptedChunk, $publicKey);
            $encryptedData .= $encryptedChunk;
        }
        // 将分段加密后的数据进行处理（例如Base64编码）并发送给接收方
        $encrypt_data = base64_encode($encryptedData);
        //输出加密后数据
        return $encrypt_data;
    }else{
        $private_key = openssl_pkey_get_private($private_key_str);
        openssl_sign($sign_string, $signature, $private_key, OPENSSL_ALGO_SHA1);
        $sign = base64_encode($signature);
        //输出签名后数据
        return $sign;
    }

  }


  /**
  发送请求
   */
  public static function use_filegetcontent(array $data,string $url){
      // 创建HTTP头部
      $options = array(
          'http' => array(
              'header' => "Content-type: application/x-www-form-urlencoded\r\n",
              'method' => 'POST',
              'content' => http_build_query($data),
          ),
      );

// 创建上下文流
      $context = stream_context_create($options);



// 发送POST请求并获取响应
      $xmlData = file_get_contents($url, false, $context);

      return $xmlData;
  }
}